import Vue from 'vue'
import VueRouter from 'vue-router'


// 导入页面
import Login from '@/views/login'
import Layout from '@/views/layout'
import Welcome from '@/views/layout/welcome'
import Chart from '@/views/layout/chart'
import User from '@/views/layout/user'
import Question from '@/views/layout/question'
import Enterprise from '@/views/layout/enterprise'
import Subject from '@/views/layout/subject'
import NotFound from '@/views/404/NotFound'
import {getToken} from '@/utils/token'
Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/layout',
    name: 'Layout',
    component: Layout,
    children: [
      {path: 'welcome',component: Welcome},
      {path: '',redirect: 'welcome'},
      {path: 'chart',component: Chart},
      {path: 'user',component: User},
      {path: 'question',component: Question},
      {path: 'enterprise',component: Enterprise},
      {path: 'subject',component: Subject},
      {path: '*',component: NotFound}
    ]
  },
  {
    path: '*',
    component: NotFound
  }
]

const router = new VueRouter({
  routes
})

/**
 * 执行：只要有路由跳转，它就会进来这里，在这里。我们就可以对它做权限判断
 * 
 * to：到哪里去
 * from：从何而来
 * next：决定能否过去，以及到哪里去
 */

 router.beforeEach((to,from,next)=>{
   if(to.path === '/login'){
     next()
   }else{
     const token = getToken()

     if(token){
       next() // 有权限
     }else{
       next('/login') // 没有权限
     }
   }
 })


export default router